/*
 ------------------------------------------------------------------------------
	sdlmedia - a test program for a media search library, including test UI.

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#include "common.h"
#include "fastheap.h"
#include "sdlmedia.h"
#include "medialib.h"

int _walk(FastHeap_t* pHeap, char* pszBase, FoundFileProc fp, void* pData)
{
	WIN32_FIND_DATAA wfd;
	HANDLE hFind;
	int ret = -1;
	
	char* pszFindPath = (char*)malloc(strlen(pszBase)+3);
	if (pszFindPath == NULL)
	{
		return 0;
	}

	strcpy(pszFindPath, pszBase);
	strcat(pszFindPath, "\\*");

	hFind = FindFirstFileA(pszFindPath, &wfd);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
			{
				if (wfd.cFileName[0] != '.')
				{
					char* pszNewBase = (char*)_fast_heap_alloc(pHeap, strlen(pszBase)+strlen(wfd.cFileName)+2);
					if (pszNewBase != NULL)
					{
						sprintf(pszNewBase, "%s\\%s", pszBase, wfd.cFileName);
						_walk(pHeap, pszNewBase, fp, pData);
					}
				}
			}
			else
			{
				fp(pData, pszBase, wfd.cFileName);
			}
		} while (FindNextFileA(hFind, &wfd));
	}

	free(pszFindPath);
	FindClose(hFind);

	return 0;
}


